



var db;







function createDB() {
    db = openDatabase("dbMap", "0.1", "A list of points.", 200000);
    if(!db){alert("Failed to connect to database.");}else{console.log('Data Base created');}

    
    db.transaction(function(tx) {
                  tx.executeSql("CREATE TABLE mPins (ident REAL UNIQUE, address TEXT, lat REAL,  longt REAL,name TEXT, type REAL)", [], null, null);
                   });

    
};

function loadPins(fPath) {

    loadJSON(fPath,
             function(data) { console.log(data); },
             function(xhr) { console.error(xhr); }
             );

};

function deletePins() {
    
        if(!db){alert("Failed to connect to database.");return;}
        db.transaction(function(tx) {
                       tx.executeSql("DELETE FROM mPins", [], null, null)});
    for (var i = 0; i < markersArray.length; i++ ) {
        markersArray[i].setMap(null);
    }
    markersArray.length = 0;
    
};
function printPins() {
    if(!db){alert("Failed to connect to database.");return;}
    db.transaction(function(tx) {
                   tx.executeSql("SELECT * FROM mPins", [], function(tx, result) {
                                 for(var i = 0; i < result.rows.length; i++) {
                                    console.log('<b>' + result.rows.item(i)['label']+'</b><br />');
                                 }}, null)});



};
function loadJSON(path, success, error)
{
    if(!db){alert("Failed to connect to database.");return;}
    var request = new XMLHttpRequest();
    request.open("GET", path, false);
    request.send(null)
    var my_JSON_object = JSON.parse(request.responseText);
    var o=my_JSON_object.mPoint;
    db.transaction(function(tx) {
                   for (var p in o) {
        
                       tx.executeSql("INSERT INTO mPins (ident , address , lat , longt ,name , type) values(?, ?, ?, ?,?,?)", [o[p].id, o[p].address, o[p].lat ,o[p].longt , o[p].name, o[p].type], null, function (tx, error) {
                                     console.log(error);
                                     });
                   
                   }
    
     });

};